/*
 * require mootools: Core Request Request.JSON
 * require bluebird
 * provide Sync
 *
 */
;(function(win){
	var Sync = win.Sync = {
		'postShare': postShare,
		'postPrint': postPrint,
		'getPrintlist': getPrintlist,
		'getPhotolist': getPhotolist,
		'postEmail': postEmail,
		'postLucky': postLucky
	};
	function postLucky(eventId){ 
		return requestJSON({ 
			url: '/lucky/' + eventId,
			method: 'post'
		});
	}
	/**
	 * @function postEmail
	 *
	 * thenable
	 * @param uriMap {String} 
	 * @param data {Object} 
	 */
	function postEmail(uriMap, data){
		if(!uriMap){
			throw new Error('uriMap is required when sending email');
		}
		return requestJSON({
					url: '/sendEmail/' + uriMap,
					method: 'post',
					data:{data: data}
				});
	}
	/**
	 * @function postShare
	 *
	 * thenable
	 * @param eventId {number} 
	 * @param shareContent {array} [1,2,3]
	 */
	function postShare(eventId, shareContent){
		if(!eventId){
			throw new Error('Event id is required when insert into sharelist!');
		}
		if(!shareContent){
			throw new Error('shareContent is required when insort into sharelist!');
		}
		var copy = [];
		shareContent = "^(" + shareContent.join('|') + ")$"
		//console.log(shareContent);
		return requestJSON({
					url: '/share/' + eventId,
					method: 'post',
					data: {
						'shareContent': shareContent
					}
				});
	}
	/**
	 * @function postPrint
	 *
	 *	thenable
	 * @param eventId {number}
	 * @param printItems {array} [{photo_id:1,copies: 1}, {photo_id:2}]
	 */
	function postPrint(eventId, printItems){
	 	if(!eventId){
			throw new Error('Event id is required when insort into printlist!');
		}
		if(!printItems){
			throw new Error('printItems is required when insort into printlist!');
		}
		return requestJSON({
					url: '/print/' + eventId,
					method:'post',
					data: {
						'printItems': printItems
					}
				});
	}
	/**
	 * @function getPrintlist
	 *
	 * thenable
	 * @param eventId {number}
	 */
	function getPrintlist(eventId){
		if(!eventId){
			throw new Error('Event id is required when asking for printlist!');
		}
		return	requestJSON({
					url: '/printlist/' + eventId,
					method: 'get',
					timeout: 10000
				});
	}
	/**
	 * @function getPhotolist 
	 *
	 * thenable
	 * @param eventId {number}
	 * @param startTime {number} timestamp
	 */
	function getPhotolist(eventId, startTime){
		if(!eventId){
			throw new Error('Event id is required when asking for photolist!');
		}
		var st = startTime || 0;
		return	requestJSON({
					url: '/photolist/' + eventId,
					method: 'get',
					timeout: 10000,
					data: {'startTime': st}
				});
	}
	/**
	 * @function request
	 *
	 * thenable
	 * a wrapper to mootools class Request using promise
	 */
	function request(options){
		return new Promise(function(resolve, reject){
			if(typeOf(options) === 'string'){
				options = {
					'url': options
				}
			}
			
			var ops = Object.append({}, options, {
				'onSuccess': resolve,
				'onFailure': reject
			});
			new Request(ops).send();
		});
	}
	/**
	 * @function requestJSON
	 * 
	 * thenable
	 * a wrapper to mootools class Request.JSON using promise
	 */
	function requestJSON(options){
		return new Promise(function(resolve, reject){
			if(typeOf(options) === 'string'){
				options = {
					'url': options
				}
			}
			
			var ops = Object.append({}, options, {
				'onSuccess': resolve,
				'onFailure': reject
			});
			//console.log(ops)
			new Request.JSON(ops).send();
		});
	}
})(window);
